package com.ruoyi.environment.service.impl;

import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.SecurityUtils;
import com.ruoyi.environment.domain.OpmEnvSewageLicense;
import com.ruoyi.environment.mapper.OpmEnvSewageLicenseMapper;
import com.ruoyi.environment.service.IOpmEnvSewageLicenseService;
import com.ruoyi.system.api.LinkFileServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 污水许可资料Service业务层处理
 *
 * @author cjie
 * @date 2021-07-23
 */
@Service
public class OpmEnvSewageLicenseServiceImpl extends LinkFileServiceImpl implements IOpmEnvSewageLicenseService {
    @Autowired
    private OpmEnvSewageLicenseMapper opmEnvSewageLicenseMapper;

    /**
     * 查询污水许可资料
     *
     * @param id 污水许可资料ID
     * @return 污水许可资料
     */
    @Override
    public OpmEnvSewageLicense selectOpmEnvSewageLicenseById(Long id) {
        OpmEnvSewageLicense result = opmEnvSewageLicenseMapper.selectOpmEnvSewageLicenseById(id);
        result.setFileList(getFileList(id.toString()));
        return result;
    }

    /**
     * 查询污水许可资料列表
     *
     * @param opmEnvSewageLicense 污水许可资料
     * @return 污水许可资料
     */
    @Override
    public List<OpmEnvSewageLicense> selectOpmEnvSewageLicenseList(OpmEnvSewageLicense opmEnvSewageLicense) {
        return opmEnvSewageLicenseMapper.selectOpmEnvSewageLicenseList(opmEnvSewageLicense);
    }

    /**
     * 新增污水许可资料
     *
     * @param opmEnvSewageLicense 污水许可资料
     * @return 结果
     */
    @Override
    public int insertOpmEnvSewageLicense(OpmEnvSewageLicense opmEnvSewageLicense) {
        opmEnvSewageLicense.setId(IdUtil.getSnowflake(0, 0).nextId());
        opmEnvSewageLicense.setCreateTime(DateUtils.getNowDate());
        opmEnvSewageLicense.setCreateBy(SecurityUtils.getUsername());
        linkFile(opmEnvSewageLicense.getFileIdList(), opmEnvSewageLicense.getId().toString());
        return opmEnvSewageLicenseMapper.insertOpmEnvSewageLicense(opmEnvSewageLicense);
    }

    /**
     * 修改污水许可资料
     *
     * @param opmEnvSewageLicense 污水许可资料
     * @return 结果
     */
    @Override
    public int updateOpmEnvSewageLicense(OpmEnvSewageLicense opmEnvSewageLicense) {
        opmEnvSewageLicense.setUpdateTime(DateUtils.getNowDate());
        opmEnvSewageLicense.setUpdateBy(SecurityUtils.getUsername());
        linkFile(opmEnvSewageLicense.getFileIdList(),Long.toString(opmEnvSewageLicense.getId()));
        return opmEnvSewageLicenseMapper.updateOpmEnvSewageLicense(opmEnvSewageLicense);
    }

    /**
     * 批量删除污水许可资料
     *
     * @param ids 需要删除的污水许可资料ID
     * @return 结果
     */
    @Override
    public int deleteOpmEnvSewageLicenseByIds(Long[] ids) {
        String[] res = new String[ids.length];
        for(int i=0;i<ids.length;i++){
            res[i] = String.valueOf(ids[i]);
        }
        deleteLinkFile(res);
        return opmEnvSewageLicenseMapper.deleteOpmEnvSewageLicenseByIds(ids);
    }

    /**
     * 删除污水许可资料信息
     *
     * @param id 污水许可资料ID
     * @return 结果
     */
    @Override
    public int deleteOpmEnvSewageLicenseById(Long id) {
        return opmEnvSewageLicenseMapper.deleteOpmEnvSewageLicenseById(id);
    }
}
